package com.tn.shirojwt.server.impl;
import com.tn.shirojwt.bean.Permission;
import com.tn.shirojwt.bean.User;
import com.tn.shirojwt.dao.PermissionDao;
import com.tn.shirojwt.dao.RoleDao;
import com.tn.shirojwt.dao.UserDao;
import com.tn.shirojwt.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @create 2020-03-16  16:08
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private PermissionDao PermissionDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public String getPassword(String username) {
        try {
           return this.userDao.findByUserName(username).getPassword();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUser(String username) {
        try {
            return this.userDao.findByUserName(username);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public Boolean updatePassword(String username, String newPassword) {
        try {
            int i = this.userDao.updatePassword(username, newPassword);
            return 1>0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<User> getUser() {
        return this.userDao.findAll();
    }

    @Override
    public Boolean banUser(String username) {
        try {
            int i = this.userDao.banUser(username);
            return 1>0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int checkUserBanStatus(String username) {
        try {
            return this.userDao.findByUserName(username).getState();
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;

    }

    @Override
    public List<Permission> finAllPermission() {
        return this.PermissionDao.findAll();
    }

}
